/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.emergency.plan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.emergency.common.constant.PlanDicConstant;
import com.hopes.bizz.emergency.common.constant.enums.ParkEmergencySysDictEnum;
import com.hopes.bizz.emergency.plan.dto.ErGovernmentEmergencyPlanDTO;
import com.hopes.bizz.emergency.plan.dto.ErGovernmentEmergencyPlanExcelDTO;
import com.hopes.bizz.emergency.plan.entity.EmergencyParkPlanEntity;
import com.hopes.bizz.emergency.plan.entity.EpEmergencyPlanFileEntity;
import com.hopes.bizz.emergency.plan.entity.ErEmergencyPlanAuditRecordEntity;
import com.hopes.bizz.emergency.plan.entity.ErGovernmentEmergencyPlanEntity;
import com.hopes.bizz.emergency.plan.mapper.ErGovernmentEmergencyPlanMapper;
import com.hopes.bizz.emergency.plan.service.EpEmergencyPlanFileService;
import com.hopes.bizz.emergency.plan.service.ErEmergencyPlanAuditRecordService;
import com.hopes.bizz.emergency.plan.service.ErGovernmentEmergencyPlanService;
import com.hopes.bizz.emergency.plan.vo.ErGovernmentEmergencyPlanExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import com.hopes.boss.common.security.util.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 政府应急预案
 *
 * @author cx
 * @date 2023-10-25 17:34:19
 */
@Service
public class ErGovernmentEmergencyPlanServiceImpl extends ServiceImpl<ErGovernmentEmergencyPlanMapper, ErGovernmentEmergencyPlanEntity> implements ErGovernmentEmergencyPlanService {

    @Resource
    private ErEmergencyPlanAuditRecordService erEmergencyPlanAuditRecordService;

    @Resource
    private EpEmergencyPlanFileService epEmergencyPlanFileService;

    @Override
    public void handlerAudit(ErGovernmentEmergencyPlanEntity erGovernmentEmergencyPlan) {
        //选择的状态
        Integer preStatus = erGovernmentEmergencyPlan.getStatus();
        UpdateWrapper<ErGovernmentEmergencyPlanEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ErGovernmentEmergencyPlanEntity::getId,erGovernmentEmergencyPlan.getId())
                .set(ErGovernmentEmergencyPlanEntity::getStatus,preStatus)
                .set(ErGovernmentEmergencyPlanEntity::getReleaseDate, LocalDate.now());

        if (PlanDicConstant.EMERGENCY_STATUS_WAIT_RELEASE.equals(preStatus)
                ||PlanDicConstant.EMERGENCY_STATUS_AUDIT_FAILED.equals(preStatus)
                ||PlanDicConstant.EMERGENCY_STATUS_ALREADY_RELEASE.equals(preStatus)){
            updateWrapper.lambda().set(ErGovernmentEmergencyPlanEntity::getAuditOpinion,erGovernmentEmergencyPlan.getAuditOpinion());
            ErEmergencyPlanAuditRecordEntity recordEntity = new ErEmergencyPlanAuditRecordEntity();
            recordEntity.setEmergencyPlanId(erGovernmentEmergencyPlan.getId());
            recordEntity.setAuditStatus(preStatus);
            recordEntity.setAuditOpinion(erGovernmentEmergencyPlan.getAuditOpinion());
            recordEntity.setAuditUser(SecurityUtils.getUser().getName());
            recordEntity.setCreateUserId(SecurityUtils.getUserId());
            erEmergencyPlanAuditRecordService.save(recordEntity);
        }
        this.update(updateWrapper);

    }

    @Override
    public Object saveGovernment(ErGovernmentEmergencyPlanDTO erGovernmentEmergencyPlan) {
        ErGovernmentEmergencyPlanEntity erGovernmentEmergencyPlanEntity = new ErGovernmentEmergencyPlanEntity();
        BeanUtils.copyProperties(erGovernmentEmergencyPlan,erGovernmentEmergencyPlanEntity);
        this.save(erGovernmentEmergencyPlanEntity);
        List<EpEmergencyPlanFileEntity> fileList = erGovernmentEmergencyPlan.getFileList();
        fileList.forEach(fileEntity -> fileEntity.setEmergencyPlanId(erGovernmentEmergencyPlanEntity.getId()));
        return epEmergencyPlanFileService.saveFile(fileList);
    }

    @Override
    public Object updateGovernment(ErGovernmentEmergencyPlanDTO erGovernmentEmergencyPlan) {
        ErGovernmentEmergencyPlanEntity erGovernmentEmergencyPlanEntity = new ErGovernmentEmergencyPlanEntity();
        BeanUtils.copyProperties(erGovernmentEmergencyPlan,erGovernmentEmergencyPlanEntity);
        this.updateById(erGovernmentEmergencyPlanEntity);
        List<EpEmergencyPlanFileEntity> fileList = erGovernmentEmergencyPlan.getFileList();
        fileList.forEach(fileEntity -> fileEntity.setEmergencyPlanId(erGovernmentEmergencyPlanEntity.getId()));
        return epEmergencyPlanFileService.updateFile(fileList);
    }

    @Override
    public ValidateImportBO<ErGovernmentEmergencyPlanExcelDTO> validateImport(List<ErGovernmentEmergencyPlanExcelDTO> excelDTOList) {
        ValidateImportBO<ErGovernmentEmergencyPlanExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ErGovernmentEmergencyPlanExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;
        // 做校验
        for (ErGovernmentEmergencyPlanExcelDTO excelDTO : excelDTOList) {
            String validateRes = ValidatorUtils.validateEntity(excelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(excelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add(validateRes);
                }}));
            }

        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ErGovernmentEmergencyPlanEntity importDtoToEntity(ErGovernmentEmergencyPlanExcelDTO excelDto) {
        ErGovernmentEmergencyPlanEntity entity = new ErGovernmentEmergencyPlanEntity();
        BeanUtils.copyProperties(excelDto, entity);
        entity.setPlanLevel(Integer.valueOf(DictResolver.getItemValueByLabel(ParkEmergencySysDictEnum.emergency_plan_level, excelDto.getPlanLevel())));
        entity.setPlanType(Integer.valueOf(DictResolver.getItemValueByLabel(ParkEmergencySysDictEnum.emergency_plan_type, excelDto.getPlanType())));
        entity.setIndustryType(Integer.valueOf(DictResolver.getItemValueByLabel(ParkEmergencySysDictEnum.emergency_industry_type, excelDto.getIndustryType())));
        entity.setStatus(Integer.valueOf(DictResolver.getItemValueByLabel(ParkEmergencySysDictEnum.emergency_status, excelDto.getStatus())));
        entity.setEstablishmentUnit(DictResolver.getItemValueByLabel(ParkEmergencySysDictEnum.emergency_compilation_org, excelDto.getEstablishmentUnit()));
        return entity;
    }

    @Override
    public ErGovernmentEmergencyPlanExcelVO exportEntityToVo(ErGovernmentEmergencyPlanEntity entity) {
        ErGovernmentEmergencyPlanExcelVO excelVO = new ErGovernmentEmergencyPlanExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }
}